package com.gnizr.snewsr;

import java.util.List;
import java.util.Date;
import org.apache.log4j.Logger;

import com.gnizr.core.bookmark.BookmarkManager;
import com.gnizr.core.link.LinkManager;
import com.gnizr.db.dao.Bookmark;
import com.gnizr.db.dao.Comment;
import com.gnizr.db.dao.DaoResult;
import com.gnizr.db.dao.comment.CommentDao;
import com.gnizr.web.action.AbstractAction;

import com.gnizr.db.dao.User;

/**
 * <p>A WebWork action that fetches information about a given Bookmark. 
 * The client must specify a valid Bookmark ID in order to fetch bookmark information.</p>
 * <p>
 * This class uses <code>BookmarkManager</code> and <code>LinkManager</code> to acquire
 * information about bookmarks. These classes are business logic implementation that
 * hides the low-level DAO operations. 
 * </p>
 * 
 * @author Harry Chen
 *
 */
public class GetBookmarkDetail extends AbstractAction{

	private static final Logger logger = Logger.getLogger(GetBookmarkDetail.class);

	private static final long serialVersionUID = 2657480501192557947L;
	
	// input from the client
	private int bookmarkId;
	
	// output to be consumed by the WebWork Result (e.g., freemarker templates)
	private Bookmark bookmark;
	private List<Bookmark> othersSaved;
	private int totalOthersSaved;

	private List<Comment> comments;
	
	private int commentCount;
	
	// Classes for DAO and business logic operations
	private BookmarkManager bookmarkManager;
	private LinkManager linkManager;
	
	private CommentDao commentDao;
	
	public int getTotalOthersSaved() {
		return totalOthersSaved;
	}

	public List<Bookmark> getOthersSaved() {
		return othersSaved;
	}

	/**
	 * @return the bookmark
	 */
	public Bookmark getBookmark() {
		return bookmark;
	}

	/**
	 * @return the bookmarkId
	 */
	public int getBookmarkId() {
		return bookmarkId;
	}

	/**
	 * @param bookmarkId the bookmarkId to set
	 */
	public void setBookmarkId(int bookmarkId) {
		this.bookmarkId = bookmarkId;
	}

	/**
	 * @return the bookmarkManager
	 */
	public BookmarkManager getBookmarkManager() {
		return bookmarkManager;
	}

	/**
	 * @param bookmarkManager the bookmarkManager to set
	 */
	public void setBookmarkManager(BookmarkManager bookmarkManager) {
		this.bookmarkManager = bookmarkManager;
	}


	@Override
	protected String go() throws Exception {
		logger.debug("FetchBookmark.go()");
		if(getBookmarkId() > 0){
			bookmark = bookmarkManager.getBookmark(getBookmarkId());
			if(bookmark != null && bookmark.getLink() != null){
				DaoResult<Bookmark> res = linkManager.pageLinkHistory(bookmark.getLink(),0,10);
				totalOthersSaved = res.getSize();
				
				DaoResult<Comment> cmt = commentDao.pageComments(bookmark,0,10);
				comments = cmt.getResult();
				
				
				othersSaved = res.getResult();
							
			}else{
				logger.debug("No such bookmark goes by the ID " + getBookmarkId());
			}
		}
		return SUCCESS;
	}

	
	public String doGetBookmarkOnly() throws Exception{
		logger.debug("FetchBookmark.go()");
		if(getBookmarkId() > 0){
			bookmark = bookmarkManager.getBookmark(getBookmarkId());
		}
		return SUCCESS;
	}

	public LinkManager getLinkManager() {
		return linkManager;
	}

	public void setLinkManager(LinkManager linkManager) {
		this.linkManager = linkManager;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public CommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public int getCommentCount() {
		commentCount = comments.size();
		return commentCount;
	}

	public void setCommentCount(int commentCount) {
		this.commentCount = commentCount;
	}

	public void setBookmark(Bookmark bookmark) {
		this.bookmark = bookmark;
	}

	public void setOthersSaved(List<Bookmark> othersSaved) {
		this.othersSaved = othersSaved;
	}

	public void setTotalOthersSaved(int totalOthersSaved) {
		this.totalOthersSaved = totalOthersSaved;
	}
}
